package com.chanjiedata.changbs.module.mine;

/**
 * 文件名 : PayGetCodeActivity
 * 创建者 : MangoWang
 * 创建日期 : 2019/11/27 17:32
 * 微信 : MangoWaWang
 * 邮箱 : 763482205@qq.com
 * 描述 : TODO
 */

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


import com.chanjiedata.changbs.R;
import com.chanjiedata.changbs.base.BaseActivity;
import com.chanjiedata.changbs.help.UserHelper;
import com.chanjiedata.changbs.net.Apis;
import com.chanjiedata.changbs.net.NetworkManager;
import com.chanjiedata.changbs.net.interfaces.OnRequestCallBack;
import com.chanjiedata.changbs.proto.Connect;
import com.chanjiedata.changbs.utlis.StatusBarUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

public class PayGetCodeActivity extends BaseActivity {

    @BindView(R.id.back_tv)
    TextView backTv;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.phone_et)
    EditText phoneEt;
    @BindView(R.id.mobile_code_et)
    EditText mobileCodeEt;
    @BindView(R.id.get_code_tv)
    TextView getCodeTv;
    @BindView(R.id.bt_sumbit)
    Button btSumbit;
    private String mPhone;
    private String type;
    private String code;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay_get_code);
        ButterKnife.bind(this);
        StatusBarUtil.setStatusBarLightMode(this, Color.parseColor("#ffffff"), true);
        type = getIntent().getStringExtra("captcha_type");
        tvTitle.setText("请先通过短信验证");
        initListener();
        mPhone = phoneEt.getText().toString();
    }

    private void initListener() {
        phoneEt.setEnabled(false);
        phoneEt.setText(UserHelper.getUser().getMember_mobile());
        backTv.setOnClickListener(view -> {
            finish();
        });
        getCodeTv.setOnClickListener(view -> {
            getCode();
        });
        btSumbit.setOnClickListener(view -> {
            validateCode();
        });

    }

    /**
     * 获取验证码
     */
    private void getCode() {
        if (TextUtils.isEmpty(mPhone)) {
            showToast("请输入手机号码");
            return;
        }
        Connect.captcha.Builder builder = Connect.captcha.newBuilder();
        builder.setPhone(mPhone);
        builder.setType(type);
        Connect.captcha data = builder.build();
        showProgress();
        NetworkManager.INSTANCE.post(Apis.getMobileCode, data.toByteArray(), new OnRequestCallBack() {
            @Override
            public void onOk(byte[] response) {

            }

            @Override
            public void onError(int code, String errorMessage) {
                hideProgress();
                if (code == 200) {
                    countDownReSend(getCodeTv, 60);
                } else {
                    showToast(errorMessage);
                }
            }
        });
    }

    /**
     * 验证验证码
     */
    private void validateCode() {
        code = mobileCodeEt.getText().toString();
        Connect.captcha.Builder builder = Connect.captcha.newBuilder();
        builder.setPhone(mPhone);
        builder.setType(type);
        builder.setCaptcha(code);
        showProgress();
        NetworkManager.INSTANCE.post(Apis.checkMobileCode, builder.build().toByteArray(), new OnRequestCallBack() {
            @Override
            public void onOk(byte[] response) {
                hideProgress();
            }
            @Override
            public void onError(int code, String errorMessage) {
                hideProgress();
                if (code == 200) {
                    Intent intent = new Intent(mContext, PayPasswordChangeOneActivity.class);
                    if (UserHelper.getUser().getIs_paypwd() == 0) {
                        intent.putExtra("activity_title", "设置支付密码");
                        intent.putExtra("message", "第一步设置支付密码，以添加银行卡");
                        intent.putExtra("title", "设置支付密码");
                    } else {
                        intent.putExtra("activity_title", "修改支付密码");
                        intent.putExtra("message", "第一步输入新的支付密码");
                        intent.putExtra("title", "修改支付密码");
                    }
                    intent.putExtra("captcha", code);
                    startActivity(intent);
                } else {
                    showToast(errorMessage);
                }

            }
        });
    }

}
